<?php
class App_Admin_Model_LangI18n extends Main_Db_Table_Abstract
{
	/**
	 * @var string
	 */
	protected $_name = 'lang_i18n';
	
	/**
	 * @var string
	 */
	protected $_primary = 'id';
	
	/**
	 * @var array
	 */	
	protected $_fields = array(
		'id',
		'lang_id',
		'name',
	);
	
	/**
	 * @param array $data
	 * @return bool
	 */
	public function insertItem($data) 
	{
		$dataOk = $this->beforeSave($data, array('lang_id', 'name', 
		), array('name'));
		
		return $this->insert($dataOk);
	}

	/**
	 * @param array $data
	 * @param int $id
	 * @return bool
	 */
	public function updateItem($data, $id) 
	{
		$dataOk = $this->beforeSave($data, array('name', 
		), array('name'));
    	
    	$rowCount = $this->getAdapter()->fetchOne("
    		SELECT 
    			COUNT(*) 
    		FROM 
    			lang_i18n 
    		WHERE 
    			lang_id={$id}
    	");
    	
    	if ($rowCount == 1) {
    		$result = $this->update($dataOk, "lang_id={$id}");
    	} else if ($rowCount == 0){
    		$dataInsert = $dataOk 
    					+ array(
    						'lang_id'	=> $id,
    					);
    		$result = $this->insert($dataInsert);
    	}
    	return $result;
	}

	/**
	 * @param int $langId
	 * @return bool
	 */
	public function deleteItemByLangId($langId) 
	{
		return $this->delete("lang_id={$langId}");
	}
	
}
